#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#define NONE                0
#define PENCIL              1
#define FILLUP              2
#define TEXT                3
#define ERASER              4
#define SELECTOR            5
#define MAGNIFIER_BOOST     6
#define MAGNIFIER_SHRINK    7

static int canvas_num;
static int cur_canva_index;
static QWidget *cur_canva;
static bool ifButtonMagnifier = false;
static bool ifLeftPressed = false;
static int cur_pen = NONE;
static int cur_width;
static QColor cur_color;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("Smart Canvas");
    QIcon *temp_icon = new QIcon;
    temp_icon->addFile(tr(":/ui/ui_sign/revoke.png"));
    ui->toolButton_revoke->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/advance.png"));
    ui->toolButton_advance->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/pencil.png"));
    ui->toolButton_pencil->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/fillup.png"));
    ui->toolButton_fillup->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/text.png"));
    ui->toolButton_text->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/eraser.png"));
    ui->toolButton_eraser->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/selector.png"));
    ui->toolButton_selector->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/magnifier.png"));
    ui->toolButton_magnifier->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/magnifier_boost.png"));
    ui->toolButton_magnifier_boost->setIcon(*temp_icon);
    temp_icon->addFile(tr(":/ui/ui_sign/magnifier_shrink.png"));
    ui->toolButton_magnifier_shrink->setIcon(*temp_icon);

    ui->toolButton_revoke->setEnabled(false);
    ui->toolButton_advance->setEnabled(false);
    ui->widget_magnifier->hide();

    ui->listWidget_tab->setFlow(QListView::LeftToRight);

    tabs *new_tab = new tabs(this);
    new_tab->setData("新建画布","0");
    connect(new_tab,SIGNAL(canva_to_close(int)),this,SLOT(deleteCanva(int)));

    QListWidgetItem *tab_item = new QListWidgetItem();
    tab_item->setSizeHint(QSize(116,22));

    ui->listWidget_tab->addItem(tab_item);
    ui->listWidget_tab->setItemWidget(tab_item,new_tab);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_listWidget_tab_currentRowChanged(int currentRow){//槽函数------------>改变当前画布

    printf("currentRow = %d\n",currentRow);
    ui->stackedWidget->setCurrentIndex(currentRow);
}

void MainWindow::deleteCanva(int canva_index){
    ui->listWidget_tab->setCurrentRow(canva_index);
    QListWidgetItem *item_to_delete = ui->listWidget_tab->currentItem();
    ui->listWidget_tab->removeItemWidget(item_to_delete);
    delete item_to_delete;

    ui->stackedWidget->setCurrentIndex(canva_index);
    QWidget *widget_to_delete = ui->stackedWidget->currentWidget();
    ui->stackedWidget->removeWidget(widget_to_delete);

    canvas_num--;
    if(cur_canva_index>canvas_num){
        cur_canva_index--;
    }
    ui->stackedWidget->setCurrentIndex(cur_canva_index);
}

//菜单栏槽函数
void MainWindow::on_action_new_triggered(){
    QWidget *new_canva = new QWidget(this);

    ui->stackedWidget->addWidget(new_canva);
    canvas_num++;
    ui->stackedWidget->setCurrentIndex(canvas_num);
    cur_canva_index = ui->stackedWidget->currentIndex();
    cur_canva = ui->stackedWidget->currentWidget();


    tabs *new_tab = new tabs(this);
    new_tab->setData(QString("新建画布_%1").arg(QString::number(canvas_num)),QString::number(canvas_num));
    connect(new_tab,SIGNAL(canva_to_close(int)),this,SLOT(deleteCanva(int)));

    QListWidgetItem *tab_item = new QListWidgetItem();
    tab_item->setSizeHint(QSize(116,22));

    ui->listWidget_tab->addItem(tab_item);
    ui->listWidget_tab->setItemWidget(tab_item,new_tab);
    ui->listWidget_tab->setCurrentRow(cur_canva_index);

    /*QPixmap *_pix = new QPixmap(size());
    _pix->fill(Qt::white);

    QPainter painter(_pix);

    painter.end();
    painter.begin(cur_canva);
    painter.drawPixmap(0,0,*_pix);*/
}

void MainWindow::on_action_open_triggered(){

}

void MainWindow::on_action_save_triggered(){

}

void MainWindow::on_action_saveAs_triggered(){

}

//工具栏槽函数
void MainWindow::on_toolButton_pencil_clicked(){
    cur_pen = PENCIL;
}

void MainWindow::on_toolButton_fillup_clicked(){
    cur_pen = FILLUP;
}

void MainWindow::on_toolButton_text_clicked(){
    cur_pen = TEXT;
}

void MainWindow::on_toolButton_eraser_clicked(){
    cur_pen = ERASER;
}

void MainWindow::on_toolButton_selector_clicked(){
    cur_pen = SELECTOR;
}

void MainWindow::on_toolButton_magnifier_clicked(){
    if(ifButtonMagnifier==false){
        ui->widget_magnifier->show();
        ifButtonMagnifier = true;
    }else if(ifButtonMagnifier==true){
        ui->widget_magnifier->hide();
        ifButtonMagnifier = false;
    }
}

void MainWindow::on_toolButton_magnifier_boost_clicked(){
    cur_pen = MAGNIFIER_BOOST;
    /*QCursor *myCursor=new QCursor(QPixmap(":/ui/ui_sign/magnifier_boost.png"),-1,-1);
    this->setCursor(*myCursor);*/
    ui->widget_magnifier->hide();
    ifButtonMagnifier = false;
}

void MainWindow::on_toolButton_magnifier_shrink_clicked(){
    cur_pen = MAGNIFIER_SHRINK;

    ui->widget_magnifier->hide();
    ifButtonMagnifier = false;
}

//监听鼠标事件
void MainWindow::mousePressEvent(QMouseEvent *ev){
    if (ev->button()==Qt::LeftButton){
        ifLeftPressed = true;
        if (cur_pen==NONE) {

        }else if (cur_pen==PENCIL) {
            struct single_line this_line;
            this_line.type = 1;
            this_line.width = cur_width;
            this_line.trail.append(ev->pos());
            lines.append(this_line);
            opts.append(PENCIL);
            opt_colors.append(cur_color);
        }else if (cur_pen==FILLUP) {

        }else if (cur_pen==TEXT) {

        }else if (cur_pen==ERASER) {

        }else if (cur_pen==SELECTOR) {

        }else if (cur_pen==MAGNIFIER_BOOST) {

        }else if (cur_pen==MAGNIFIER_SHRINK) {

        }
        this->update();
    }


}

void MainWindow::mouseMoveEvent(QMouseEvent *ev){

}

void MainWindow::mouseReleaseEvent(QMouseEvent *ev){

}


void MainWindow::paintEvent(QPaintEvent *ev){
    QPixmap *_pix = new QPixmap(size());
    _pix->fill(Qt::white);

    QPainter painter(_pix);

    painter.end();
    painter.begin(this);
    painter.drawPixmap(0,0,*_pix);
}
